/**
 * 定义命名空间
 */
(function()
{

	window.CCR = {};
	CCR.Collections = {}; // 有序组合
	CCR.Models = {}; // 实体类
	CCR.Views = {}; // 视图类
	CCR.Comm = {}; // 公共方法

	// 百度电台的配置信息
	CCR.Config = {
		isDebug : true,
		// 百度电台相关信息
		app_id : 424275,
		client_id : "GDfGE9SoVRim841jg1FBcFtj",
		client_secret : "Ok9BZ29wW088TcpVwfq6k5VcojaDmFAP",
		client_token : "",

		/*
		 * Request
		 * URL:https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=&client_secret=
		 */
		radio_token_url : "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials",

		/*
		 * https://openapi.baidu.com/rest/2.0/music/radio/catalog?access_token=
		 */
		radio_catalog_url : "https://openapi.baidu.com/rest/2.0/music/radio/catalog?",

		/*
		 * https://openapi.baidu.com/rest/2.0/music/radio/songlist?channelid=6&page_no=7&page_size=50&access_token=
		 */
		radio_songlist_url : "https://openapi.baidu.com/rest/2.0/music/radio/songlist?",

		/*
		 * https://openapi.baidu.com/rest/2.0/music/song/info?songid=65865126&access_token=
		 */
		radio_song_url : "https://openapi.baidu.com/rest/2.0/music/song/info?",

		/*
		 * 获取电台目录URL
		 */
		getCatalogUrl : function()
		{
			var token = this.getToken();

			var url = this.radio_catalog_url;

			if (token) {
				url += "access_token=" + token.access_token;
			}

			return url;
		},

		/*
		 * 获取访问令牌
		 */
		getToken : function()
		{
			var self = this;
			if (self.client_token) {

				return self.client_token;
			}else{
				self.initToken();
				log("Error => get token null");
				return null;
			}
			return token;
		},
		initToken:function(callback){
			var self = this;
			var url = self.radio_token_url;
			url += "&client_id=" + self.client_id + "&client_secret="
					+ self.client_secret;

			$.ajax({
				type : "get",
				async : false,
				url : url,
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				cache : false,
				success : function(data)
				{
					// 返回的数据用data.d获取内容
					self.client_token = token = data;
					typeof callback==="function"&&callback(self.client_token);
				},
				error : function(err)
				{
					log("get token error");
				}
			});
		}
	};
})();

//Global log function
function log(info) {
	if(CCR.Config.isDebug){
	info
			&& $("#infoBoard").html(
					Now() + " " + info + "</br>" + $("#infoBoard").html());
	}
}

window.onerror = function(msg, url, line) {
	var ErrorMsg = "Error=" + msg + "</br>URL=" + url + "</br>LINE=" + line
			+ "</br>";
	log(ErrorMsg);
};

var Now = function() {
	var d = new Date(), h, m, s;
	d.getHours() < 10 ? h = "0" + d.getHours() : h = d.getHours();
	d.getMinutes() < 10 ? m = "0" + d.getMinutes() : m = d.getMinutes();
	d.getSeconds() < 10 ? s = "0" + d.getSeconds() : s = d.getSeconds();
	var t = "[" + h + ":" + m + ":" + s + "]";
	return t;
};

function DebugAlert(info){
	if(CCR.Config.isDebug){
		var tar = $("#debugAlert");
		tar.html(info);
		tar.css({"display":"block"});
		setTimeout(function(){
			tar.css({"display":"none"});
		},500);
	}
}
